package com.LC.Config;

import com.LC.Service.SpringSecurityUserService;
import com.LC.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;


@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {


    @Autowired
    SpringSecurityUserService securityUserService;

    /**
     * 配置SpringSecurity相关信息
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //开放静态资源访问权限
        http.authorizeRequests()
                .antMatchers("/login.html").permitAll()
                .antMatchers("/assets/**").permitAll()
                .antMatchers("/css/**").permitAll()
                .antMatchers("/fonts/**").permitAll()
                .antMatchers("/img/**").permitAll()
                .antMatchers("/js/**").permitAll()
                .antMatchers("/plugins/**").permitAll()
                .antMatchers("/favicon.ico").permitAll()
                .antMatchers("/pages/**").authenticated();

        //csrf：对应CsrfFilter过滤器
        //disabled：是否启用CsrfFilter过滤器，如果使用自定义登录页面需要关闭此项，否则登录操作会被禁用（403）
        http.csrf().disable();

        //设置在页面可以通过iframe访问受保护的页面(默认为不允许访问)
        http.headers().frameOptions().sameOrigin().httpStrictTransportSecurity().disable();
        http.headers().frameOptions().disable();

        //登出成功后跳转登录页面
        http.logout().logoutUrl("/logout").logoutSuccessUrl("/login.html").invalidateHttpSession(true);

        //没有权限自动跳转登录页面，定制登录页
        http.formLogin().loginPage("/login.html").usernameParameter("username").passwordParameter("password")
        .loginProcessingUrl("/login").defaultSuccessUrl("/pages/main.html").failureForwardUrl("/login.html");

    }

    /**
     * 认证用户的来源
     * @param auth
     * @throws Exception
     */
    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(securityUserService).passwordEncoder(passwordEncoder());
    }

    @Bean
    public BCryptPasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}





























